package fr.cephb.joperon.core.iterator;

import java.util.Iterator;

import com.sleepycat.db.DatabaseEntry;
import com.sleepycat.db.DatabaseException;

import fr.cephb.util.Pair;

/**
 * An iterator extracting a <b>T</b> value from a delegate iterator
 * iterator of the key/values of a BerkeleyDB
 * @author lindenb
 *
 * @param <T> the object to extract from the pair(key,value) of DatabaseEntry
 */
public abstract class DatabaseValueIterator<T>
	extends DelegateIterator<Pair<DatabaseEntry, DatabaseEntry>,T>
	{

	public DatabaseValueIterator
		(
		Iterator<Pair<DatabaseEntry, DatabaseEntry>> delegate) {
		super(delegate);
		}
	
	@Override
	protected final T extractValue(Pair<DatabaseEntry, DatabaseEntry> input) {
		try
			{
			return extractFromEntries(input.first(),input.second());
			}
		catch(DatabaseException err)
			{
			close();
			throw new RuntimeException(err);
			}
		}
	/** returns the value from a pair(key,value) of the delegate Iterator */
	protected abstract T extractFromEntries(DatabaseEntry  key,DatabaseEntry  value) throws DatabaseException;
	}
